﻿////
//对象的方法与属性一律小写
function hjtimer(astepfun){
    this._interval=0;
    this._second=-1;
    if(astepfun){
        this._stepfun=astepfun;
    }else{
        this._stepfun=0;
    } 
	this.onstep=function(){ 
		this._second++;  
		if(typeof(this._stepfun)=="function"){
			this._stepfun(this, this._second, this.tostring());
		}
	}
	this.time=function(){
	    return this._second;
	}
	this.timeformated=function(){
	    return this.formattime(this._second);
	}
	this.tostring=function(){
	    return this.formattime(this._second);
	}
	this.formattime=function(atime){
		if(atime<60){
			return "00:"+this.numtemplate(atime);
		}else if(atime<3600){
			return this.numtemplate(Math.floor(atime/60)) + ":" + this.numtemplate(atime % 60);
		} 
	}
	this.numtemplate=function(anum){
		if(anum<10){
			return "0"+anum;
		}else{
			return ""+anum;
		}
	}
	//
	this.start=function(){
		if(this._interval!=0){
			window.clearInterval(this._interval);
		}
        this._second=-1;
		var _this=this;
		var t_onstep=function(){
			_this.onstep();
		}
		this._interval = window.setInterval(t_onstep, 1000, this);
        _this.onstep();
	}
	//
	this.stop=function(){
		if(this._interval!=0){
			window.clearInterval(this._interval);
		}
	}
	//
	this.pause=function(){
		if(this._interval!=0){
			window.clearInterval(this._interval);
		}
	}
	//
	this.resume=function(){
		if(this._interval!=0){
			window.clearInterval(this._interval);
		}
	}
}  